﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace FollowUpEmail
{
    using System.Configuration;
    using KSSModel;
    using KSSOnline.Controller;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Analysis delay document");
            var dataContext = new KSSDBDataContext();
            try
            {
                var watingA1 = from ds in dataContext.tblDocs
                               join u in dataContext.tblUsers on ds.approver1 equals u.UserName
                               where ds.status == 0 && ds.approved1Date == null && (DateTime.Today - ds.createdDate.Date).Days >= 2
                               select ds;
                var watingA2 = from ds in dataContext.tblDocs
                               join u in dataContext.tblUsers on ds.approver1 equals u.UserName
                               where ds.status == 10 && ds.approved2Date == null && (DateTime.Today - ds.approved1Date.Value.Date).Days >= 2
                               select ds;
                var watingTQM = from ds in dataContext.tblDocs
                                join u in dataContext.tblUsers on ds.approver1 equals u.UserName
                                where ds.status == 20 && ds.approvedQMDate == null && (DateTime.Today - ds.approved2Date.Value.Date).Days >= 2
                                select ds;
                string error = "";
                string sendEmailDetail = "";

                Console.WriteLine("sending email");
                foreach (tblDoc item in watingA1)
                {
                    sendEmail(item);
                }

                foreach (var item in watingA2)
                {
                    sendEmail(item);
                }

                foreach (var item in watingTQM)
                {
                    sendEmail(item);
                }
                Console.WriteLine("sent email completed");
            }
            catch (Exception ex)
            {

            }
        }

        private static bool sendEmail(tblDoc doc)
        {
            string emailTitle = string.Empty;
            string emailBody = string.Empty;
            string docTitle = doc.title;
            string ownerName = doc.createdByName;
            string docId = doc.id;
            string emailTo = string.Empty;
            string sendMailError = "";
            switch (doc.status)
            {
                case 0:
                    emailTo = UserHelper.GetUserbyUserName(doc.approver1).Email;
                    emailTitle = ConfigurationManager.AppSettings["newDocEmailTitel1"];
                    emailTitle = string.Format(emailTitle, docId);
                    emailBody = System.Net.WebUtility.HtmlDecode(ConfigurationManager.AppSettings["newDocEmailBody1"]);
                    emailBody = string.Format(emailBody, docId, ownerName, docTitle);
                    break;
                case 10:
                    emailTo = UserHelper.GetUserbyUserName(doc.approver2).Email;
                    emailTitle = ConfigurationManager.AppSettings["newDocEmailTitel1"];
                    emailTitle = string.Format(emailTitle, docId);
                    emailBody = System.Net.WebUtility.HtmlDecode(ConfigurationManager.AppSettings["newDocEmailBody1"]);
                    emailBody = string.Format(emailBody, docId, ownerName, docTitle);
                    break;

                case 20:
                    emailTo = UserHelper.getEmailTQMByLocation(doc.createdBy);
                    emailTitle = ConfigurationManager.AppSettings["newDocEmailTitel2"];
                    emailTitle = string.Format(emailTitle, docId);
                    emailBody = System.Net.WebUtility.HtmlDecode(ConfigurationManager.AppSettings["newDocEmailBody2"]);
                    emailBody = string.Format(emailBody, docId, ownerName, docTitle);
                    break;

                case 30:
                    emailTo = UserHelper.GetUserbyUserName(doc.createdBy).Email;
                    emailTitle = ConfigurationManager.AppSettings["newDocEmailTitelApprove"];
                    emailTitle = string.Format(emailTitle, docId);
                    emailBody = System.Net.WebUtility.HtmlDecode(ConfigurationManager.AppSettings["newDocEmailBodyApprove"]);
                    emailBody = string.Format(emailBody, ownerName, docId, docTitle);
                    break;

                case 11:
                    emailTo = UserHelper.GetUserbyUserName(doc.createdBy).Email;
                    emailTitle = ConfigurationManager.AppSettings["newDocEmailTitelReject"];
                    emailTitle = string.Format(emailTitle, docId);
                    emailBody = System.Net.WebUtility.HtmlDecode(ConfigurationManager.AppSettings["newDocEmailBodyReject"]);
                    emailBody = string.Format(emailBody, ownerName, docId, docTitle);
                    break;

                default:
                    return false;
            }
            //emailTo = "redharing_555@hotmail.com";

            return Utilities.Email.SendMail(ConfigurationManager.AppSettings["emailFrom"],
                                emailTo,
                             emailTitle,
                                emailBody,
                                out sendMailError);
        }
    }
}
